package cn.edu.njust.service.impl;

import cn.edu.njust.service.SystemService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletContext;

import java.util.Calendar;
import java.util.TimerTask;

/**
 * TODO
 */
public class MyTask extends TimerTask {
    // 晚上0点刷新数据库
    private static final int C_SCHEDULE_HOUR = 0;
    private static boolean isRunning = false;
    private final ServletContext context;

    @Resource
    private SystemService systemService;

    public MyTask(ServletContext context) {
        this.context = context;
    }

    public void run() {
        Calendar cal = Calendar.getInstance();
        if (!isRunning) {
            if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY)) {
                isRunning = true;
                context.log("开始执行指定任务");

                systemService.freshAllStatus();
                isRunning = false;
                context.log("指定任务执行结束");
            }
        } else {
            context.log("上一次任务执行还未结束");
        }
    }
}
